#include "apue.h"
#include <sys/wait.h>
#include "error.c"

int main(int argc, char const *argv[])
{
    char    buf[MAXLINE], filename[FILENAME_MAX], *args[32];
    pid_t   pid;
    int     status;

    printf("%% ");
    while(fgets(buf, MAXLINE, stdin) != NULL){
       if( buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = 0;
        
        if((pid = fork()) < 0){
            err_sys("fork error"); //父进程创子进程失败。
        }else if (pid == 0){ //子进程
            // execlp("ls\0", "ls\0", "-l", "Makefile", (char *)0); //执行并替换当前进程
            execlp(buf, buf, (char*)0);
            err_ret("couldn't execute: %s", buf); //替换失败则会执行这里
            exit(127);
        }
        
        if((pid = waitpid(pid, &status, 0) < 0))
            err_sys("waitpid error");

        printf("%% ");
    }

    return 0;
}
